﻿// See https://aka.ms/new-console-template for more information
using Furion;
using Furion.DependencyInjection;
using Furion.EventBus;
using Furion.EventBus.RabbitMq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Diagnostics;

var provider = Inject.Create().BuildServiceProvider(false);

// 启动 eventbus hostedservice
var hosts = provider.GetService<IHostedService>()!;
hosts.StartAsync(new CancellationToken(false));

// 发布启动消息
var publisher = provider.GetService<IEventPublisher>()!;
publisher.PublishAsync(new RabbitMqEventSource("duan", "我启动了")).GetAwaiter();

while (true)
{
    Console.Write("publish the input message : ");
    publisher.PublishAsync(new RabbitMqEventSource("duan", Console.ReadLine() ?? ""));
}

namespace MyNamespace
{
    public class DuanSubscriber : IEventSubscriber, ISingleton
    {
        [EventSubscribe("duan")]
        public Task Subduan(EventHandlerExecutingContext context)
        {
            Debug.WriteLine("receive message : " + context.Source.Payload);
            return Task.CompletedTask;
        }
    }
}
